//
//  LMHWaterFallController.m
//  WaterFallDemo
//
//  Created by 刘梦桦 on 2017/5/20.
//  Copyright © 2017年 lmh. All rights reserved.
//

#import "LMHWaterFallController.h"
#import "LMHWaterFallLayout.h"
#import "LMHShop.h"
#import "LMHShopCell.h"
#import <MJRefresh.h>
//#import <MJExtension.h>
#import "THJXSQGDetailVC.h"

static NSString * const LMHShopId = @"shop";

@interface LMHWaterFallController ()<UICollectionViewDataSource, LMHWaterFallLayoutDeleaget,UICollectionViewDelegate>
/** 所有的商品数据 */
@property (nonatomic, strong) NSMutableArray  * shops;

/** <#name#> */
@property (nonatomic, weak) UICollectionView * collectionView;

/** 列数 */
@property (nonatomic, assign) NSUInteger columnCount;


@end

@implementation LMHWaterFallController
#pragma mark - 懒加载
- (NSMutableArray *)shops{
    if (!_shops) {
        _shops = [NSMutableArray array];
    }
    return _shops;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initialize];
    
    [self setupLayoutAndCollectionView];
    
    [self setupRefresh];
    if(KIsiPhoneX){
        [self setXNavBarWithTitle:@"限时抢购"];
    }else{
        [self setNavBarWithTitle:@"限时抢购"];
    }
//    [self setUpNav];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.hidden = YES;
//   self.navigationController.navigationBar.barTintColor = COLOR(83, 84, 85, 1);
    self.navigationController.navigationBar.backgroundColor = [UIColor blackColor];
}
/**
 * 初始化
 */
- (void)initialize{
    self.title = @"限时抢购";
    self.view.backgroundColor = [UIColor whiteColor];
    self.view.frame = CGRectMake(0, NavbarHeight, ScreenWidth, ScreenHeight);
}
/**
 * 创建布局和collectionView
 */
- (void)setupLayoutAndCollectionView{
    // 创建布局
    LMHWaterFallLayout * waterFallLayout = [[LMHWaterFallLayout alloc]init];
    waterFallLayout.delegate = self;
//    waterFallLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    // 创建collectionView
    UICollectionView * collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0,NavbarHeight, ScreenWidth, ScreenHeight-NavbarHeight) collectionViewLayout:waterFallLayout];
    collectionView.backgroundColor = COLOR(243, 243, 243, 1);
    collectionView.delegate = self;
    collectionView.dataSource = self;
//    UIView * bgview = [[UIView alloc] init];
//    bgview.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
//    bgview.backgroundColor = SingleColor(whiteColor);
//    [self.view addSubview:bgview];
    [self.view addSubview:collectionView];
    // 注册
    [collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([LMHShopCell class]) bundle:nil] forCellWithReuseIdentifier:LMHShopId];
    self.collectionView = collectionView;
}

/**
 * 刷新控件
 */
- (void)setupRefresh{
    
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewShops)];
    self.collectionView.mj_header.backgroundColor = [UIColor whiteColor];
    [self.collectionView.mj_header beginRefreshing];
    
    
//    self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreShops)];
//    self.collectionView.mj_footer.backgroundColor = [UIColor whiteColor];
//    self.collectionView.mj_footer.hidden = YES;
}

/**
 * 加载新的商品
 */
- (void)loadNewShops{
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

////        NSArray * shops = [LMHShop mj_objectArrayWithFilename:@"shop.plist"];
//        for(NSInteger i = 0;i<30;i++){
//            LMHShop * model = [[LMHShop alloc] init];
//            model.img = @"http://s13.mogujie.cn/b7/bao/131012/vud8_kqywordekfbgo2dwgfjeg5sckzsew_310x426.jpg_200x999.jpg";
//            model.price = [NSString stringWithFormat:@"%ld",i];
//            model.w = 200;
//            if(i%2){
//                model.h = 240;
//            }else{
//                model.h = 210;
//            }
//            [self.shops addObject:model];
//        }
////        [self.shops removeAllObjects];
//
////        [self.shops addObjectsFromArray:shops];
//
//        // 刷新表格
//        [self.collectionView reloadData];
//
//        [self.collectionView.mj_header endRefreshing];
////    });
    __weak typeof(self) weakself = self;
    NSString * url = @"http://p.thjb.net/thjblink/appdata.php?action=mall_list";
    [THJNetWork getNetDataFromUrl:url callback:^(id receiveData, NSError *error) {
//        NSLog(@"ll-%@");
        if([receiveData[@"status"]isEqual:@"success"]){
            
            weakself.shops = receiveData[@"data"][@"list"];
            [self.collectionView reloadData];
            [self.collectionView.mj_header endRefreshing];
        }
    }];
}
#pragma mark UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
//    self.collectionView.mj_footer.hidden = self.shops.count == 0;
    
    return self.shops.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    LMHShopCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:LMHShopId forIndexPath:indexPath];
    cell.backgroundColor = SingleColor(whiteColor);
    NSDictionary * itemDic = self.shops[indexPath.item];
    cell.layer.masksToBounds = YES;
    cell.layer.cornerRadius = 6;
    cell.shop = itemDic;
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"点击商品---+++++++========");
    NSDictionary * itemDic = self.shops[indexPath.item];
    THJXSQGDetailVC * vc = [THJXSQGDetailVC new];
    vc.itemid = itemDic[@"itemid"];
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark  - <LMHWaterFallLayoutDeleaget>
- (CGFloat)waterFallLayout:(LMHWaterFallLayout *)waterFallLayout heightForItemAtIndexPath:(NSUInteger)indexPath itemWidth:(CGFloat)itemWidth{
    
//    LMHShop * shop = self.shops[indexPath];
//
//    return itemWidth * shop.h / shop.w;
    return 200;
}

- (CGFloat)rowMarginInWaterFallLayout:(LMHWaterFallLayout *)waterFallLayout{
    
    return 10;
    
}
- (CGFloat)columnMarginInWaterFallLayout:(LMHWaterFallLayout *)waterFallLayout{
    return 5;
}
- (NSUInteger)columnCountInWaterFallLayout:(LMHWaterFallLayout *)waterFallLayout{
    
    return 2;
    
}
@end
